package com.lmx.project.controller;


import com.lmx.project.common.BaseResponse;
import com.lmx.project.common.ErrorCode;
import com.lmx.project.common.ResultUtils;
import com.lmx.project.exception.BusinessException;
import com.lmx.project.model.entity.Comfile;
import com.lmx.project.service.ComfileService;
import com.lmx.project.untils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/confile")
@Slf4j
public class ComfileController {
    @Resource
    private ComfileService comfileService;

    //    @Resource
//    private FileUntil fileUntil;
    @Resource
    private FileUtil fileUtil;
    private String resoursedir = "resourse/";

    /**
     * 增加文件
     */
    @PostMapping
    public BaseResponse<Long> addFile(MultipartFile file) throws IOException {
        if (file == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请上传文件");
        }
        Comfile comfile = new Comfile();

        String originalFilename = file.getOriginalFilename();
        comfile.setName(originalFilename);
//            后缀名
//        String substring = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());

//        String resultfilename = UUID.randomUUID().toString().replace("-", "");
        String b = fileUtil.saveMultipartFile(file, resoursedir);


        comfile.setFileurl(b);
        comfileService.save(comfile);
        return ResultUtils.success(comfile.getId());
    }

    /**
     * 删除文件
     */
    @DeleteMapping
    public BaseResponse<Boolean> DeleteFile(@RequestParam("ids") List<Long> ids) throws IOException {
        if (ids == null || ids.size() == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<Comfile> comfiles = comfileService.listByIds(ids);

        List<String> collect = comfiles.stream().map(Comfile::getFileurl).collect(Collectors.toList());

        fileUtil.removeFileList(collect);


        boolean b = comfileService.removeBatchByIds(ids);


        return ResultUtils.success(b);
    }


    @PutMapping()
    public BaseResponse<Long> UpdateFile(Long id, MultipartFile file) throws IOException {

        if (id == null || id == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (file == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请上传文件");
        }
//        删除该文件
        ArrayList<Long> longs = new ArrayList<>();
        longs.add(id);
        DeleteFile(longs);

        Comfile comfile = new Comfile();
        comfile.setId(id);
        String originalFilename = file.getOriginalFilename();
        comfile.setName(originalFilename);
//            后缀名
        String b = fileUtil.saveMultipartFile(file, resoursedir);


        comfile.setFileurl(b);
        comfileService.updateById(comfile);
        return ResultUtils.success(comfile.getId());
    }


    /**
     * 根据资源文件的id获取资源
     */
    @PostMapping("list")
    public BaseResponse<List<Comfile>> getComfile(@RequestBody List<Long> list) {
        if (list == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<Comfile> comfiles = comfileService.listByIds(list);

        String preUrl = fileUtil.getPreUrl();
        comfiles.stream().forEach(comfile -> {
            comfile.setFileurl(preUrl + comfile.getFileurl());
        });

        return ResultUtils.success(comfiles);
    }

}
